﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web;
using StattiLibrary;

namespace Stage9.Statti.PownceLibrary
{
    public class Pownce : IPlugin
    {
        #region IPlugin Members

        public void Post(string message, string username, string password)
        {
            String url = "http://api.pownce.com/2.0/send/message.xml?app_key=4xpi6979252838ji6jcb1bscie64i029";
            WebRequest wreq = WebRequest.Create(url);
            String userpass = username + ":" + password;
            CredentialCache cache = new CredentialCache();
            cache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
            wreq.Credentials = cache;
            wreq.Headers.Add("Authorization", "Basic" + Convert.ToBase64String(new ASCIIEncoding().GetBytes(userpass)));
            wreq.ContentType = "application/x-www-form-urlencoded";
            wreq.Method = "POST";
            String param = "note_to=public&note_body=" + message;
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(param);
            wreq.ContentLength = bytes.Length;
            try
            {
                Stream os = wreq.GetRequestStream();
                os.Write(bytes, 0, bytes.Length);
                os.Close();
                WebResponse resp = wreq.GetResponse();
                StreamReader sr = new StreamReader(resp.GetResponseStream());
                Console.WriteLine(sr.ReadToEnd());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        public string Name
        {
            get
            {
                return "Pownce Plugin";
            }
        }

        public string Version
        {
            get
            {
                return "0.1";
            }
        }

        public string Author
        {
            get
            {
                return "David Uvalle";
            }
        }

        public string Description
        {
            get
            {
                return "This is the pown wrapper API for Statti";
            }
        }

        #endregion
    }
}
